﻿package com.sh.game.loader
{
    import com.sh.game.consts.*;
    import com.sh.game.global.*;
    import flash.display.*;
    import flash.events.*;
    import flash.system.*;
    import flash.utils.*;
    import morn.core.handlers.*;

    public class PluginLoader extends Object
    {
        private var moduleInfo:Object;
        private var _name:String;
        private var moduleurl:String;
        private var _onProgress:Function;
        private var _onComplete:Function;

        public function PluginLoader()
        {
            return;
        }// end function

        public function loadPlugin(param1:String, param2, param3:Function, param4:Function, param5:Boolean = false) : void
        {
            this._name = param1;
            this._onProgress = param3;
            this._onComplete = param4;
            if (param2 is String)
            {
                this.moduleurl = param2;
                App.maploader.lock();
                App.mloader.lock();
                App.mloader2.lock();
                App.leisureMloader.lock();
                App.mloader3.loadBYTE(Config.getUrl(this.moduleurl, ResourceType.SWF), 99, new Handler(this.completedHandler), new Handler(this.usermoduleProgress), null, false);
            }
            else if (param2 is ByteArray)
            {
                this.completedHandler(param2);
            }
            return;
        }// end function

        protected function usermoduleError(event:Event) : void
        {
            this._onComplete({name:this._name, module:null, error:1});
            return;
        }// end function

        protected function loadSwfCompletedHandler(event:Event) : void
        {
            event.currentTarget.removeEventListener(Event.COMPLETE, this.completedHandler);
            event.currentTarget.removeEventListener("progress", this.usermoduleProgress);
            event.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, this.usermoduleError);
            this._onComplete({name:this._name, module:event.currentTarget.content});
            if (this.moduleInfo is Loader)
            {
                (this.moduleInfo as Loader).unloadAndStop(false);
            }
            this.moduleInfo = null;
            this._onProgress = null;
            this._onComplete = null;
            this._name = null;
            this.moduleurl = null;
            return;
        }// end function

        protected function usermoduleProgress(param1) : void
        {
            this._onProgress({name:this._name, progress:int(param1 * 100)});
            return;
        }// end function

        protected function completedHandler(param1) : void
        {
            var _loc_2:ByteArray = null;
            var _loc_4:String = null;
            var _loc_5:ByteArray = null;
            var _loc_6:int = 0;
            var _loc_7:ByteArray = null;
            var _loc_8:int = 0;
            App.maploader.unlock();
            App.mloader.unlock();
            App.mloader2.unlock();
            App.leisureMloader.unlock();
            if (param1 == null)
            {
                this.usermoduleError(null);
                return;
            }
            if (param1 is Event)
            {
                _loc_2 = param1.currentTarget.data;
                param1.currentTarget.removeEventListener(Event.COMPLETE, this.completedHandler);
                param1.currentTarget.removeEventListener("progress", this.usermoduleProgress);
                param1.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, this.usermoduleError);
            }
            else
            {
                _loc_2 = param1;
            }
            if (Config.Encrypt)
            {
                _loc_2.position = 0;
                _loc_4 = "8aff474e62f692ad1002b591a4b59794";
                _loc_5 = new ByteArray();
                _loc_5.writeMultiByte(_loc_4, "utf8");
                _loc_6 = _loc_2.length / 2;
                _loc_7 = new ByteArray();
                _loc_8 = 0;
                while (_loc_8 < _loc_5.length)
                {
                    
                    _loc_7.writeByte(_loc_2[_loc_8 + _loc_6] ^ _loc_5[_loc_8]);
                    _loc_8++;
                }
                _loc_2.position = _loc_6;
                _loc_2.writeBytes(_loc_7);
            }
            this.moduleInfo = new Loader();
            var _loc_3:* = new LoaderContext(false, ApplicationDomain.currentDomain);
            this.moduleInfo.contentLoaderInfo.addEventListener(Event.COMPLETE, this.loadSwfCompletedHandler);
            this.moduleInfo.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.usermoduleError);
            this.moduleInfo.loadBytes(_loc_2, _loc_3);
            _loc_2.clear();
            return;
        }// end function

    }
}
